<?php

namespace app\shop\controller;


use think\Db;
use think\Request;
use think\Exception;
use app\shop\model\Workset as WorksetModel;
use app\shop\model\Worktype as WorktypeModel;
use app\crm\model\Staff as StaffModel;
use app\shop\model\Workother as WorkotherModel;

class Workreportset extends \ShopController
{
      /**
     * 员工简单列表接口
     */
    public function easy(Request $request){
        $StaffModel = new \app\crm\model\Staff();

        try {
            $params = $request->get();

            $data = $StaffModel->easySearch($params);
            $man=db("crm_report_staff")->where(["is_manager"=>1])->column("staff_id");
            foreach ($data as $k => $v) {
                # code...
                if(in_array($v["staff_id"], $man)){
                    $data[$k]["is_checked"]=true;
                }else{
                    $data[$k]["is_checked"]=false;
                }
            }
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    public function easy2(Request $request){
        $StaffModel = new \app\crm\model\Staff();
        try {
            $params = $request->get();
            $only=$params["onlyneed"];
            if($only==="true"){
               // echo 1;
                $man=db("crm_report_check_role")->column("staff_id");
                
                if($man){
                    foreach ($man as $k => $v) {
                        # code...
                        $man[$k]="'{$v}'";
                    }
                    $man=implode(',',$man);
                }
                $data = $StaffModel->easySearch2($params,$man);
            }else{
                // echo 2;
                $data = $StaffModel->easySearch($params);
            }
            
            
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    public function updatecould(Request $request){
        try {
            $post = $request->param();
            $model=new WorkotherModel();
            $model->deletebyuser($post["staff_id"]);
            $model->add($post);
            $this->successTerminate($post);
        }catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    //获取可查看情况
    public function getcouldlist(Request $request){
        try {
            $post = $request->param();
            $model=new WorkotherModel();
            $data=$model->getuserchek($post["staff_id"]);
            $depart=[];
            $user=[];
            $length=0;
            if($data){
                if($data["staff_list"]){
                    $user=db("crm_staff")->where("staff_id in ({$data["staff_list"]})")->field("staff_id,staff_name,head_img")->select();
                    $length+=count($user);
                }
                if($data["depart_list"]){
                    $depart=db("crm_department")->where("department_id in ({$data["depart_list"]})")->field("department_id,department_name")->select();
                    $length+=count($depart);
                }
            }
            $back["user"]=$user;
            $back["length"]=$length;
            $back["depart"]=$depart;
            $this->successTerminate($back);
        }catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    public function updateman(Request $request){
        try {
            $params = $request->post();
            $manarr=json_decode($params["jsonstr"],true);
            $model=new WorksetModel();
            db("crm_report_staff")->where(["is_manager"=>1])->delete();
            if($manarr){
                $data=[];
                foreach ($manarr as $k => $v) {
                    # code...
                    $data[$k]["staff_id"]=$v;
                    $data[$k]["is_manager"]=1;
                }
            }
            db("crm_report_staff")->insertAll($data);

            
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    public function getset(Request $request){
        try {
            $post = $request->param();
            $model=new WorksetModel();
            $work=$model->getset($post["title"]);
            if($work)
                $post["state"]=1;
            else
                $post["state"]=0;
            $this->successTerminate($post);
        }catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
       
        }
    }
    public function setWorkPicture($set){
       
        try {
            $model=new WorksetModel();
            $model->setpic($set);
            $post["success"]=1;
            $this->successTerminate($post);
        }catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
       
        }
    }
    public function setCrmCus($set){
        try {
            $model=new WorksetModel();
            $model->setcrm($set);
            $post["success"]=1;
            $this->successTerminate($post);
        }catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
       
        }
    }
    public function del(Request $request){
        try {
            $post = $request->param();
            $model=new WorktypeModel();
            $model->delbyid($post["id"]);
            $this->successTerminate($post);
        }catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
       
        }
    }
    public function updatetype(Request $request){
        try {
            $post = $request->param();
            $model=new WorktypeModel();
            $model->updatebyid($post["id"],$post["title"]);
            $this->successTerminate($post);
        }catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
       
        }
    }
    //新增标签
    public function addtype(Request $request){
        try {
            $post = $request->param();
            $model=new WorktypeModel();
            $data["title"]=trim($post["title"]);
            $data["cuser"]=$post["cuser"];
            $data["ctime"]=date("Y-m-d H:i:s");
            $data["is_delete"]=0;
            //验证
            if(!$data["title"]){
                \exception("名称不能为空");
            }else{
                $has=$model->checktitle($data["title"]);
                if($has){
                    \exception("标签已存在");
                }
            }

            
            $id=$model->newtype($data);
            if($id){
                $back["id"]=$id;
                $back["title"]=$data["title"];
                $this->successTerminate($back);
            }else{
                \exception("系统繁忙");
            }
            
        }catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
       
        }
    }
    //标签列表
    public function getlist(){
        try {
            $model=new WorktypeModel();
            $list=$model->getlist();
            $this->successTerminate($list);
        }catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
       
        }

    }
}
